using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;

namespace SyncApp.RemoteProvider.RemoteSyncProviders
{
    public abstract class AppRemoteProviderBase : DbServerSyncProvider, IAppRemoteProvider
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly Action Initializer;


        /// <summary>
        /// Initializes a new instance of the <see cref="AppRemoteProviderBase"/> class.
        /// </summary>
        protected AppRemoteProviderBase(ChangeTrackingType changeTrackingType)
        {
            Initializer = InitializeSyncAdapters;

            //Create a connection to the sample server database.
            var connection = new SqlConnection(Properties.Settings.Default.ConnectionString);
            Connection = connection;

            Initializer();
            
        }

        public abstract void InitializeSyncAdapters();

        protected void AddSyncAdapter(string table,SyncDirection syncDirection)
        {
            //by default, assume bidirectional mode
            Func<string,SyncAdapter> helper = BidirectionalSyncAdpaterHelper;


            switch (syncDirection)
            {
                case (SyncDirection.Snapshot):
                    helper = SnapshotSyncAdpaterHelper;
                    break;
                case (SyncDirection.DownloadOnly):
                    helper = DownloadOnlySyncAdpaterHelper;
                    break;
                case (SyncDirection.UploadOnly):
                    helper = UploadOnlySyncAdpaterHelper;
                    break;
            }

            SyncAdapters.Add(helper(table)); 

        }

        protected abstract SyncAdapter BidirectionalSyncAdpaterHelper(string table);
        protected abstract SyncAdapter DownloadOnlySyncAdpaterHelper(string table);
        protected abstract SyncAdapter UploadOnlySyncAdpaterHelper(string table);
        protected abstract SyncAdapter SnapshotSyncAdpaterHelper(string table);
    }
}